Micron Document
🎖️GitЯра🎖️

Node / meshtastic / Meshtastic-Android / files / core / data / src / commonMain / kotlin / org / meshtastic / core / data / util / SingleFlightRefresher.kt

Displaying Raw • Download

core/data/src/commonMain/kotlin/org/meshtastic/core/data/util/SingleFlightRefresher.kt 01cd54907d62cd90913d3d071b6bc240cae365ef (01cd5490) Text, 3.40 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.data.util

Tff7b72import T7ee787co.touchlab.kermit.Logger
Tff7b72import T7ee787kotlinx.coroutines.CoroutineScope
Tff7b72import T7ee787kotlinx.coroutines.Deferred
Tff7b72import T7ee787kotlinx.coroutines.SupervisorJob
Tff7b72import T7ee787kotlinx.coroutines.async
Tff7b72import T7ee787kotlinx.coroutines.sync.Mutex
Tff7b72import T7ee787kotlinx.coroutines.sync.withLock
Tff7b72import T7ee787kotlinx.coroutines.withTimeoutOrNull
Tff7b72import T7ee787org.meshtastic.core.common.util.safeCatching
Tff7b72import T7ee787kotlin.coroutines.CoroutineContext

T8b949e/**
* Runs [fetchAndPersist] as a single shared flight on a detached, app-lifetime scope: concurrent callers join the
* in-flight refresh instead of starting their own, a caller that stops waiting (cancellation or [refresh]'s
* `maxWaitMs`) can't abort it, and — critically — no lock a cache-read path can see is ever held across the network
* call. api.meshtastic.org routinely takes 20-60s (and minutes during outages); holding a caller-visible mutex across
* that request has wedged cached emissions, and the UI with them, before.
*
* [fetchAndPersist] performs the network call and writes results to the local cache as a side-effect; its failures are
* caught and logged here, so callers observe refresh results only through the cache. The owner is expected to be an
* app-lifetime singleton — the internal SupervisorJob is never cancelled.
*/
Tff7b72internal Tff7b72class T56d364SingleFlightRefresherTb4b4b4(
Te6edf3contextTb4b4b4: Te6edf3CoroutineContextTb4b4b4,
Tff7b72private Tff7b72val Te6edf3tagTb4b4b4: Tffa657StringTb4b4b4,
Tff7b72private Tff7b72val Te6edf3fetchAndPersistTb4b4b4: Te6edf3suspend Tb4b4b4(Tb4b4b4) Tff7b72-Tff7b72> Tffa657UnitTb4b4b4,
Tb4b4b4) Tb4b4b4{

T8b949e/** Guards [inFlight] so concurrent callers share one refresh. */
Tff7b72private Tff7b72val Te6edf3guard Tff7b72= Te6edf3MutexTb4b4b4(Tb4b4b4)

Tff7b72private Tff7b72var Te6edf3inFlightTb4b4b4: Te6edf3DeferredTff7b72<Tffa657UnitTff7b72>Tff7b72? Tff7b72= Tff7b72null

Tff7b72private Tff7b72val Te6edf3scope Tff7b72= Te6edf3CoroutineScopeTb4b4b4(Te6edf3context Tff7b72+ Te6edf3SupervisorJobTb4b4b4(Tb4b4b4)Tb4b4b4)

T8b949e/**
* Starts (or joins) the shared refresh. When [maxWaitMs] is set, the caller waits at most that long before falling
* back to cached data; the refresh itself always runs to completion, bounded only by the HttpClient's own
* timeout/retry policy.
*/
Tff7b72suspend Tff7b72fun Td2a8ffrefreshTb4b4b4(Te6edf3maxWaitMsTb4b4b4: Tffa657Long? Tff7b72= Tff7b72nullTb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3refresh Tff7b72=
Te6edf3guardTb4b4b4.Te6edf3withLock Tb4b4b4{
Te6edf3inFlightTff7b72?.Te6edf3takeIf Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3isActive Tb4b4b4}
Tff7b72?: Te6edf3scope
Tb4b4b4.Te6edf3async Tb4b4b4{
Te6edf3safeCatching Tb4b4b4{ Te6edf3fetchAndPersistTb4b4b4(Tb4b4b4) Tb4b4b4}
Tb4b4b4.Te6edf3onFailure Tb4b4b4{ Te6edf3e Tff7b72-Tff7b72> Te6edf3LoggerTb4b4b4.Te6edf3wTb4b4b4(Te6edf3eTb4b4b4) Tb4b4b4{ Ta5d6ff"Tffd700$Te6edf3tagTa5d6ff: network refresh failedTa5d6ff" Tb4b4b4} Tb4b4b4}
Tffa657Unit
Tb4b4b4}
Tb4b4b4.Te6edf3also Tb4b4b4{ Te6edf3inFlight Tff7b72= Tffa657it Tb4b4b4}
Tb4b4b4}
Tff7b72if Tb4b4b4(Te6edf3maxWaitMs Tff7b72=Tff7b72= Tff7b72nullTb4b4b4) Tb4b4b4{
Te6edf3refreshTb4b4b4.Te6edf3joinTb4b4b4(Tb4b4b4)
Tb4b4b4} Tff7b72else Tff7b72if Tb4b4b4(Te6edf3withTimeoutOrNullTb4b4b4(Te6edf3maxWaitMsTb4b4b4) Tb4b4b4{ Te6edf3refreshTb4b4b4.Te6edf3joinTb4b4b4(Tb4b4b4) Tb4b4b4} Tff7b72=Tff7b72= Tff7b72nullTb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3w Tb4b4b4{ Ta5d6ff"Tffd700$Te6edf3tagTa5d6ff: refresh still in flight after Tffd700${Te6edf3maxWaitMsTffd700}Ta5d6ffms; using cached dataTa5d6ff" Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}

Served by rngit 1.5.0 - Generated in 0.04s